package com.hua.giftool.ui.components

import android.content.Context
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import coil.ImageLoader
import coil.compose.rememberAsyncImagePainter
import coil.decode.GifDecoder
import coil.request.ImageRequest
import coil.size.Size

@Composable
fun <T : Context> T.GifImage(
    @DrawableRes gifResource: Int,
    modifier: Modifier = Modifier,
    @StringRes contentDescription: Int? = null
) {
    val imageLoader =
        ImageLoader.Builder(this).components {
            add(GifDecoder.Factory())
        }.build()
    Image(
        painter = rememberAsyncImagePainter(
            model = ImageRequest.Builder(this)
                .data(gifResource)
                .size(Size.ORIGINAL)
                .build(),
            imageLoader = imageLoader
        ),
        contentDescription = contentDescription?.let { stringResource(it) },
        modifier = modifier
    )
}